//
//  WithdrawalDetailsViewController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2025/1/9.
//  Copyright © 2025 GCY. All rights reserved.
//

#import "WithdrawalDetailsViewController.h"
#import "WithdrawalTiXingTableViewCell.h"
#import "WithdrawalCententCell.h"
#import "WithdrawalTypeCell.h"
#import "WithdrawalLoserCell.h"
#import "WithdrawalFileCell.h"

@interface WithdrawalDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong , nonatomic)UITableView *tableView;
@property (strong , nonatomic)NSMutableArray *dataArray;
@property (strong, nonatomic) NSString *type;
@property (strong, nonatomic) NSDictionary *dict;
@end

@implementation WithdrawalDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"提现详情";
    
    self.view.backgroundColor = BackgroundColor;
    self.type = @"1";
    [self networking];
    
}
- (void)networking{
    NSDictionary *params = @{
        @"code": self.code,
    };
    
    NSString *urlString = [NSString stringWithFormat:@"%@/ntocc-basic-api/gdMerchant/queryWithdrawDetail", [ReAddressDynamic shared].baseUrl];
    [HTTPManager getNetworkRequest:urlString withParameter:params withVC:self netName:@"提现详情" success:^(id response, NSString *msg) {
        self.type = response[@"withdrawStatus"];
        self.dict = response;
        if (self.dict) {
            [self.view addSubview:self.tableView];
        }
//        [self.tableView reloadData];
        } failure:^(NSInteger code, NSString *msg) {
            
        }];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *titArr = [@[] mutableCopy];
    NSMutableArray *numberArr =  [@[] mutableCopy];
    if (self.dict) {
        [titArr addObject:@"交易金额"];
        [titArr addObject:@"交易单号"];
        [titArr addObject:@"创建时间"];
        [titArr addObject:@"提现账户"];
        [numberArr addObject:CJStringWithFormat(@"%@",self.dict[@"transferAmount"])];
        [numberArr addObject:CJStringWithFormat(@"%@",self.dict[@"bizOrderNo"])];
        [numberArr addObject:CJStringWithFormat(@"%@",self.dict[@"gmtCreated"])];
        [numberArr addObject:CJStringWithFormat(@"%@",self.dict[@"cardNumber"])];
    }
    
    if ([self.type isEqualToString:@"1"]) {
        if (indexPath.section == 0) {
            WithdrawalTiXingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalTiXingTableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }else if(indexPath.section == 1){
            WithdrawalCententCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalCententCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.nameLabel.text = titArr[indexPath.row];
            cell.cententNLabel.text = numberArr[indexPath.row];
            return cell;
        }else{
            WithdrawalTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalTypeCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if ([self.dict[@"withdrawStatus"] intValue] == 1) {
                cell.typeImagView.image = [UIImage imageNamed:@"sussesImag"];
                cell.TypeNameLabel.textColor =[UIColor colorWithRed:82 / 255.0 green:179 / 255.0 blue:170 / 255.0 alpha:1 / 1.0];
                cell.TypeNameLabel.text = @"提现申请成功";
                cell.timeLabel.text = CJStringWithFormat(@"%@",self.dict[@"gmtCreated"]);
            }else if([self.dict[@"withdrawStatus"] intValue] == 4){
                cell.typeImagView.image = [UIImage imageNamed:@"tossImagView"];
                cell.TypeNameLabel.textColor =[UIColor colorWithRed:237 / 255.0 green:107 / 255.0 blue:134 / 255.0 alpha:1 / 1.0];
                cell.TypeNameLabel.text = @"提现失败";
                cell.timeLabel.text = CJStringWithFormat(@"%@",self.dict[@"gmtCreated"]);
            }else{
                cell.typeImagView.image = [UIImage imageNamed:@"sussesImag"];
                cell.TypeNameLabel.textColor =[UIColor colorWithRed:82 / 255.0 green:179 / 255.0 blue:170 / 255.0 alpha:1 / 1.0];
                cell.TypeNameLabel.text = @"提现申请成功";
                cell.timeLabel.text =CJStringWithFormat(@"%@",self.dict[@"gmtCreated"]);
            }
            return cell;
        }
    }else{
        if (indexPath.section == 0) {
            WithdrawalCententCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalCententCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.nameLabel.text = titArr[indexPath.row];
            cell.cententNLabel.text = numberArr[indexPath.row];
            return cell;
        }else if(indexPath.section == 1){
            WithdrawalTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalTypeCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if ([self.dict[@"withdrawStatus"] intValue] == 1) {
                cell.typeImagView.image = [UIImage imageNamed:@"sussesImag"];
                cell.TypeNameLabel.textColor =[UIColor colorWithRed:82 / 255.0 green:179 / 255.0 blue:170 / 255.0 alpha:1 / 1.0];
                cell.TypeNameLabel.text = @"提现申请成功";
                cell.timeLabel.text = self.dict[@"gmtCreated"];
            }else if([self.dict[@"withdrawStatus"] intValue] == 4){
                cell.typeImagView.image = [UIImage imageNamed:@"tossImagView"];
                cell.TypeNameLabel.textColor =[UIColor colorWithRed:237 / 255.0 green:107 / 255.0 blue:134 / 255.0 alpha:1 / 1.0];
                cell.TypeNameLabel.text = @"提现失败";
                cell.timeLabel.text = self.dict[@"gmtCreated"];
            }else{
                cell.typeImagView.image = [UIImage imageNamed:@"sussesImag"];
                cell.TypeNameLabel.textColor =[UIColor colorWithRed:82 / 255.0 green:179 / 255.0 blue:170 / 255.0 alpha:1 / 1.0];
                cell.TypeNameLabel.text = @"提现申请成功";
                cell.timeLabel.text = self.dict[@"gmtCreated"];
            }
            return cell;
        }else if(indexPath.section == 2){
            WithdrawalFileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalFileCell"];
            cell.nameLabel.text = [NSString stringWithFormat:@"%@", self.dict[@"failureReason"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            WithdrawalLoserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalLoserCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.shibaiLabel.text = self.dict[@"fixedErrorMsg"];
            cell.cententHeight.constant =  [self getHeightByWidth:(ScreenW - 90) title:[NSString stringWithFormat:@"%@", self.dict[@"fixedErrorMsg"]] font:[UIFont systemFontOfSize:15]];
            return cell;
        }
    }
    
}
- (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return ceil(height);
}
- (CGFloat)getSectionHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return ceil(height);
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.type isEqualToString:@"1"]) {
        if (section == 1) {
            return 4;
        }else{
            return 1;
        }
    }else{
        if (section == 0) {
            return 4;
        }else{
            return 1;
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([self.type isEqualToString:@"1"]) {
        return 3;
    }else{
        return 4;
    }
  
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.type isEqualToString:@"1"]) {
        if (indexPath.section == 2) {
            return 80;
        }else{
            return 50;
        }
    }else{
        if (indexPath.section == 0) {
            return 50;
        }else  if (indexPath.section == 1) {
            return 80;
        }else if(indexPath.section == 2){
            return  [self getSectionHeightByWidth:(ScreenW - 20) title:[NSString stringWithFormat:@"%@", self.dict[@"failureReason"]] font:[UIFont systemFontOfSize:15]] + 10;

        }else if(indexPath.section == 3){
            return  [self getHeightByWidth:(ScreenW - 90) title:[NSString stringWithFormat:@"%@", self.dict[@"fixedErrorMsg"]] font:[UIFont systemFontOfSize:15]] + 30;
        }else{
            return 50;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}
#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NavHeight - 45);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = BackgroundColor;
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:@"WithdrawalTiXingTableViewCell" bundle:nil] forCellReuseIdentifier:@"WithdrawalTiXingTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"WithdrawalCententCell" bundle:nil] forCellReuseIdentifier:@"WithdrawalCententCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"WithdrawalTypeCell" bundle:nil] forCellReuseIdentifier:@"WithdrawalTypeCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"WithdrawalTypeCell" bundle:nil] forCellReuseIdentifier:@"WithdrawalTypeCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"WithdrawalLoserCell" bundle:nil] forCellReuseIdentifier:@"WithdrawalLoserCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"WithdrawalFileCell" bundle:nil] forCellReuseIdentifier:@"WithdrawalFileCell"];
        
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


@end
